/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.srdxeditor.dialogs;

import net.miblounge.gradecalculator.core.descriptors.courses.Course;
import net.miblounge.gradecalculator.core.descriptors.courses.IndividualCourse;
import net.miblounge.gradecalculator.guicommon.dialogs.AbstractDialog;
import net.miblounge.gradecalculator.guicommon.interfaces.IValidationListener;
import net.miblounge.gradecalculator.guicommon.layouts.VerySimpleLayout;
import net.miblounge.gradecalculator.guicommon.widgets.CourseEditor;
import net.miblounge.miglayout.qt.MigLayout;

import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QWidget;

public class CourseDialog extends AbstractDialog implements IValidationListener {
	private final Course course;
	private CourseEditor courseEditor;
	
	public CourseDialog(final QWidget parent, final Course course)
	{
		super(parent);
		setWindowTitle(tr("Course"));
		
		this.course = course;

		setClientArea(createClientArea());
		
		courseEditor.setStartFocus();
		revalidate();
	}
	
	private QWidget createClientArea()
	{
		final QGroupBox result = new QGroupBox(tr("Course"), this);
		final VerySimpleLayout outerLayout = new VerySimpleLayout(result);
		final QWidget container = new QWidget(result);
		outerLayout.setWidget(container);

		final MigLayout innerLayout = new MigLayout("", "[][grow,fill]"); 
		container.setLayout(innerLayout);
		
		if (course instanceof IndividualCourse) {
			courseEditor = new CourseEditor(container, (IndividualCourse)course, null);
		} else {
			courseEditor = new CourseEditor(container, course);
		}

		innerLayout.addWidget(courseEditor, "grow");
		result.setLayout(outerLayout);
		return result;
	}
	
	public boolean validate()
	{
		if (!courseEditor.validate()) {
			return false;
		}
		
		return super.validate();
	}
	
	public void apply()
	{
		super.apply();
		courseEditor.apply();
	}
}
